import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import G6 from '@antv/g6'
// import 'element-plus/lib/theme-chalk/index.css';

Vue.config.productionTip = false
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

Vue.use(mavonEditor)

Vue.use(ElementUI);
//6.引入导出的路由
import router from './router'



import JsonViewer from 'vue-json-viewer'
import store from './store'; // 引入刚刚创建的store
Vue.use(JsonViewer)


import axios from 'axios'
import language from '@/languages/addApi-language.js'

// 引入 vue-quill-editor
import VueQuillEditor from 'vue-quill-editor'

// 引入 quill 样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// 使用 vue-quill-editor
Vue.use(VueQuillEditor)

const publicRoutes = ['/home', '/about', '/watch', '/tools', '/ditails', '/developerTalk', '/english', '/PrivacyPolicy', '/service-error', '/devtools','/login'];

//如果路由不在白名单中，则需要登录才能访问

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token'); // 从 localStorage 获取 token

  // 如果目标路由不在 publicRoutes 中
  if (!publicRoutes.includes(to.path)) {
    // 如果没有 token，跳转到登录页面
    if (!token) {
      next('/login');
    } else {
      // 如果有 token，继续访问目标路由
      next();
    }
  } else {
    // 如果目标路由在 publicRoutes 中，直接访问
    next();
  }
});
axios.interceptors.request.use(config => {
  const currentPath = '/home'; // 获取当前路由路径
  const token = localStorage.getItem('token'); // 从 localStorage 获取 token

  //if (!publicRoutes.includes(currentPath)) {
    
      config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token'); // 假设你存储token在localStorage中
      config.headers['Content-Type'] = 'application/json;charset=UTF-8';
      config.headers['Accept'] = 'application/json';
      config.headers['Access-Control-Allow-Origin'] = '*';
      
   
  //}
  return config;
}, error => {
  return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response && error.response.status === 401) {
      // 处理401错误，表示登录失效
      localStorage.removeItem('token'); // 清除token
      localStorage.removeItem('account'); // 清除用户名
      router.push('/login'); // 重定向到登录页面
      console.log("token失效");
    }
    return Promise.reject(error);
  }
);




const messages = {
  en: {
    heroTitle: 'Eterno - Developer’s API Paradise',
    heroDesc: 'We are Eterno, a community platform dedicated to providing free and efficient API query and calling services for global developers.',
    exploreBtn: 'Explore APIs',
    featuresTitle: 'Why Choose Eterno?',
    feature1Title: 'Rich API Resources',
    feature1Desc: 'Covering various industries, meeting different development needs.',
    feature2Title: 'Free & Open',
    feature2Desc: 'All APIs are provided free of charge, no need to worry about costs.',
    feature3Title: 'Easy to Use',
    feature3Desc: 'Detailed documentation and sample code for quick API calls.',
    feature4Title: 'Developer Community',
    feature4Desc: 'An active community to exchange ideas and grow together.',
    testimonialsTitle: 'Developer Voices',
    contactTitle: 'Contact Us',
    contactDesc: 'For any questions or cooperation intentions, please feel free to contact us.',
    footerCopyright: 'Copyright © Eterno All Rights Reserved.'
  },
  zh: {
    heroTitle: 'E特尔诺 - 开发者的API乐园',
    heroDesc: '我们是E特尔诺，一个致力于为全球开发者提供免费、高效API查询与调用服务的社区平台。',
    exploreBtn: '探索API',
    featuresTitle: '为什么选择E特尔诺？',
    feature1Title: '海量API资源',
    feature1Desc: '涵盖各行业领域，满足不同开发需求。',
    feature2Title: '免费开放',
    feature2Desc: '所有API均免费提供，无需担心成本问题。',
    feature3Title: '简单用',
    feature3Desc: '详细的文档与示例代码，快速上手调用API。',
    feature4Title: '开发者交流',
    feature4Desc: '活跃的社区，与同行交流心得，共同成长。',
    testimonialsTitle: '开发者心声',
    contactTitle: '联系我们',
    contactDesc: '有任何问题或合作意向，请随时联系我们。',
    footerCopyright: 'Copyright © E特尔诺 All Rights Reserved.'
  }
};

Vue.prototype.$global = {
  isFirstVisit: true, // 全局变量
  //全局语言切换
  language: 'en',
};






new Vue({
  router,
  language,
  store,
  
  render: h => h(App),
}).$mount('#app')
